<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/9/5
 * Time: 00:09
 */
namespace App\Http\Controllers\Web;

use App\Handlers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Services\CreditCardHandleApplyService;
use Illuminate\Http\Request;

class CreditCardHandleController extends Controller
{
    use ApiResponse;

    protected $creditCardHandleApplyService;

    public function __construct(CreditCardHandleApplyService $creditCardHandleApplyService)
    {
        $this->creditCardHandleApplyService = $creditCardHandleApplyService;
    }

    /**
     * 获取申请页面
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function apply(Request $request)
    {
        $appUrl = config('app.url');
        $param = $request->param;
        return view('web.creditCardHandle.apply', compact('appUrl', 'param'));
    }

    /**
     * 提交申请
     * @param Request $request
     * @return bool|\Illuminate\Http\JsonResponse
     */
    public function applySubmit(Request $request)
    {
        try {
            $params = convertUrlQuery(decrypt($request->param));
            return $this->creditCardHandleApplyService->apply($params['invite_user_id'], $request->mobile,
                $request->verification_code, $request->true_name,
                $request->id_card, $params['credit_card_handle_id']);
        } catch (\Exception $e) {
            die('{"code":400,"msg":"' . $e->getMessage() . '","message":"无效参数"}');
        }
    }
}